<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Db;
use think\Model as ThinkModel;

//表模型
class CommissionLog extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_COMMISSION_LOG__';
    protected $name = 'qt_commission_log';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * @param $user_id 用户id
     * 、、* @param $type 类型 1今天 2 昨天 3 本月 4 上月  5 指定时间段
     * @param $start_time
     * @param $end_time
     * @return CommissionLog[]|\think\Collection
     *
     */
    public function achievement($user_id, $start_time, $end_time, $type = '1')
    {
        if (empty($start_time)) {
            $start_time = strtotime(date('Y-m-d 00:00:00'));
        }
        if (empty($end_time)) {
            $end_time = strtotime(date('Y-m-d 23:59:59'));
        }
        $where = array();
        $where1 = array();
        $where2 = array();
        if ($type == '1') {
            $where[] = ['created_time', 'between', [$start_time, $end_time]];
            $where1[] = ['m.created_time', 'between', [$start_time, $end_time]];
            $where2[] = ['m.created_time', 'between', [$start_time, $end_time]];
        } else {
            $where[] = ['updated_time', 'between', [$start_time, $end_time]];
            $where1[] = ['m.updated_time', 'between', [$start_time, $end_time]];
            $where2[] = ['m.updated_time', 'between', [$start_time, $end_time]];
        }
        $top = $this->where('user_id', $user_id)
            ->field('sum(money) as all_money,sum(commission) as all_commission,type')
            //->where($where)
            ->where('type', 'in', ['1', '2','3','4','5','7'])
            ->group('type')
            ->select();
        $num = $this->where('user_id', $user_id)
            ->field('count(id) as num,type')
            ->where($where)
            ->where('type', 'in', ['1', '2', '3'])
            ->group('type')
            ->select();
        $tui = $all = $this->where('user_id', $user_id)
            ->where('type', 1)
            ->where($where)
            ->sum('commission');
        $all = $this->where('user_id', $user_id)
            ->where($where)
            ->sum('commission');
        $is_pay = $this->where('user_id', $user_id)
            ->field('count(id) as total,is_pay')
            ->where($where)
            ->group('is_pay')
            ->select();
        $zhu = $this->alias('m')
            ->join('qt_project p', 'p.id=m.product_id')
            ->where('m.user_id', $user_id)
            ->where($where)
            ->field('count(m.product_id) as num,p.type')
            ->group('p.type')
            ->select();
        $pai = $this->where('user_id', $user_id)
            ->field('count(id) as pai,status')
            ->where($where)
            ->where('type', '1')
            ->group('status')
            ->select();


        $project = $this->alias('m')
            ->join('qt_project p', 'p.id=m.product_id')
            ->join('qt_room r', 'm.room_id=r.id')
            ->where('m.user_id', $user_id)
            ->where('m.type', 1)
            ->where($where1)
            ->field('r.name as room_name,p.name as project_name,m.created_time,m.commission')
            ->select()->each(function ($item) {
                $item['created_time'] = ST($item['created_time']);
                return $item;
            });
        $all_project = $this->alias('m')
            ->join('qt_project p', 'p.id=m.product_id')
            ->join('qt_room r', 'm.room_id=r.id')
            ->where('m.user_id', $user_id)
            ->where('m.type', 1)
            ->where($where1)
            ->field('r.name as room_name,p.name as project_name,m.created_time,m.commission')
            ->sum('m.commission');
        $goods = $this->alias('m')
            ->join('qt_room r', 'm.room_id=r.id')
            ->join('qt_goods g', 'g.id=m.product_id')
            ->where('m.user_id', $user_id)
            ->where('m.type', 2)
            ->where($where2)
            ->field('r.name as room_name,g.name as goods_name,m.created_time,m.commission')
            ->select()->each(function ($item) {
                $item['create_time'] = ST($item['created_time']);
                return $item;
            });
        $all_goods = $this->alias('m')
            ->join('qt_room r', 'm.room_id=r.id')
            ->join('qt_goods g', 'g.id=m.product_id')
            ->where('m.user_id', $user_id)
            ->where('m.type', 2)
            ->where($where2)
            ->field('r.name as room_name,g.name as goods_name,m.created_time,m.commission')
            ->sum('m.commission');
        $card = $this->alias('m')
            ->join('qt_viper_card_recharge_log p', 'p.id = m.order_project_id')
            ->join('qt_viper_card c', 'c.id= p.viper_card_id')
            ->where('m.user_id', $user_id)
            ->whereIn('m.type', [3,4,5,7])
            ->where($where2)
            ->field('c.card_no,p.amount,m.created_time,m.commission')
            ->select()->each(function ($item) {
                $item['create_time'] = ST($item['created_time']);
                return $item;
            });
        $all_card = $this->alias('m')
            ->join('qt_viper_card_recharge_log p', 'p.id=m.order_project_id')
            ->join('qt_viper_card c', 'c.id= p.viper_card_id')
            ->where('m.user_id', $user_id)
            ->whereIn('m.type', [3,4,5,7])
            ->where($where2)
            ->field('c.card_no,p.amount,m.created_time,m.commission')
            ->sum('m.commission');
        $info = array(
            'top' => $top,//头部三个
            'num' => $num,// 详情数量
            'tui' => $tui,//推荐奖励
            'all' => $all,//提成合计
            'ispay' => $is_pay,//已支付 未支付
            'zhu' => $zhu,//主副项
            'pai' => $pai,//排钟
            'project' => $project,//项目
            'all_project' => $all_project,//项目累计
            'goods' => $goods,//商品
            'all_goods' => $all_goods,//商品合计
            'card' => $card, //售卡提成
            'all_card' => $all_card //全部售卡提成
        );
        return $info;
    }

}